<template>
	<view>
		<!-- 收货地址 -->
		<view class="center700 address white-bj pad-t-20  pad-b-20 pad-l-20 pad-r-20 border-r-20 box-sizing mar-top30" @tap="open('add')">
			<template v-if="address.id">
				<view class="you a-center j-sp">
					<view class="icon-position main-zt"></view>
					<view class="mar-left15">
						<view class="font-26 font-w-b line-h35">{{address.name}} {{address.tel}}</view>
						<view class="font-22 line-h32 black50-zt mar-top10">{{address.province}}{{address.city}}{{address.district}}{{address.area}}{{address.address}}{{address.house_number}}</view>
					</view>
					<view class="icon-jinru black60-zt"></view>
				</view>
			</template>
			<template v-else>
				<view class="a-center j-sp wu">
					<view class="a-center">
						<view class="icon-position main-zt border-r overflow-hidden d-a-j-c"></view>
						<view class="font-26 font-w-b mar-left15">请添加或选择服务地址/收货地址</view>
					</view>
					<view class="icon-jinru black60-zt"></view>
				</view>
			</template>
		</view>
		<!-- 商品信息 -->
		<view class="center710 box-sizing shop white-bj pad-t-40  pad-b-40 pad-l-30 pad-r-30 mar-top20">
			<view class="a-center">
				<view class="logo overflow-hidden border-r-5"><image :src="formData.business_logo" mode="widthFix" lazy-load></image></view>
				<view class="font-28 mar-left15 font-w-b">{{formData.business_name}}</view>
			</view>
			<view class="d-flex mar-top20">
				<view class="left border-r-10 overflow-hidden"><image :src="formData.pic" mode="widthFix" lazy-load></image></view>
				<view class="right mar-left30">
					<view class="font-26 text-overflow font-w-b">{{formData.name}}</view>
					<view class="font-22 black60-zt mar-top20">规格: {{formData.guige_name}}</view>
					<view class="mar-top15 a-center flex-wrap">
						<view class="font-20 white-zt weixin-bj border-r-5 line-h32 pad-l-10 pad-r-10 mar-right15" v-if="formData.scoredkmaxset>=0">可参与积分折扣</view>
					</view>
					<view class="a-center j-sp mar-top15">
						<view class="a-end">
							<view class="activeColor font-24 font-w-b">¥<text class="font-34">{{formData.sell_price}}</text></view>
							<view class="black60-zt font-24 text-dec-line mar-left20" v-if="formData.market_price!='0'||formData.market_price!=''">¥{{formData.market_price}}</view>
						</view>
						<view class="font-28 black40-zt">
							<text>x</text>
							<text class="mar-left5">{{formData.sum}}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="mar-top50">
				<view class="a-center j-sp font-26 mar-bottom50" v-if="formData.type==2">
					<view>配送方式</view>
					<view>
						<text>{{formData.freight_name}}</text>
					</view>
				</view>
				<view class="a-center j-sp font-26 mar-bottom50">
					<view>商品总金额</view>
					<view class="activeColor mar-right10 font-w-b">
						<text>¥</text>
						<text> {{formData.product_price}}</text>
					</view>
				</view>
				<view class="a-center j-sp font-26 mar-bottom50" v-if="formData.type==2">
					<view>运费</view>
					<view class="activeColor mar-right10 font-w-b">
						<text>+ ¥</text>
						<text> {{formData.freight_price}}</text>
					</view>
				</view>
				<view class="a-center j-sp font-26 mar-bottom50" v-if="formData.leveldk_money>0">
					<view>会员折扣({{pro.leveldk_money}} 折扣)</view>
					<view class="activeColor mar-right10 font-w-b">
						<text>- R$</text>
						<text> {{formData.leveldk_money}}</text>
					</view>
				</view>
				<view class="a-center j-sp font-26 mar-bottom50">
					<view>可用优惠券</view>
					<view @tap="openTan('Desconto','')" >
						<text class="font-22 line-h38 pad-l-15 pad-r-15 border-r-10" v-if="!couponData.id">{{coupon_list.length}}优惠券可用</text>
						<text class="font-22 activeColor line-h38 text-overflow wid-70" v-else>{{couponData.couponname}}</text>
						<text class="icon-jinru mar-left15 black50-zt"></text>
					</view>
				</view>
				<view class="font-28 mar-bottom20">
					<view>备注:</view>
					<view class="mar-top30">
						<textarea placeholder="请填写备注内容..." v-model="formData.freight_dataval" placeholder-class="font-26" class="border-1-91 wid-100 box-sizing"></textarea>
					</view>
				</view>
				<view class="font-28">
					<radio-group @change="changePay" class="pay-box">
						<view class="item a-center j-sp mar-top30" v-for="(item,index) in paylist" :key="index" v-if="item.id==1">
							<view class="a-center">
								<view class="img border-r-5 overflow-hidden"><image :src="item.pic" mode="widthFix" lazy-load></image></view>
								<view class="mar-left15 ">
									<view class="font-w-b font-28">{{item.name}}</view>
								</view>
							</view>
							<label class="radio"><radio :value="item.id" :checked="checked==item.id" style="transform: scale(0.6);"/></label>
						</view>
					</radio-group>
				</view>
			</view>
		</view>
		<!-- 积分抵扣 -->
		<view class="wid-100 shop white-bj pad-t-40  pad-b-40 pad-l-30 pad-r-30 mar-top20 box-sizing" v-if="formData.scoredkmaxset>=0">
			<view class="font-26 font-w-b">{{score.score}} 积分可兑换 <text class="activeColor mar-left10">¥{{score.score2money}}</text></view>
			<view class="a-center mar-top30">
				<view class="border-r ljm-checkbox" @tap="change" :class="[checkbox?'ljm-checkbox-active':'']"><text class="icon-duihao white-zt"></text></view>
				<view class="font-26 line-h40 mar-left10" @tap="change">使用积分折扣</view>
			</view>
		</view>
		<view class="heightTop"></view>
		<view class="heightTop"></view>
		<!-- 底部操作 -->
		<view class="foot position-fixed p-bottom wid-100 white-bj a-center j-sp pad-l-30 pad-r-30 pad-t-30 pad-b-30 box-sizing">
			<view>
				<view class="font-28 font-w-b activeColor line-h40">¥<text class="font-40">{{formData.totalprice}}</text></view>
				<view class="font-24 black60-zt line-h35">优惠金额: ¥{{formData.preferential}}</view>
			</view>
			<view class="main-bj white-zt font-26 line-h60 pad-l-30 pad-r-30 border-r" @tap="addOrder">立即支付</view>
		</view>
		<!-- 弹窗 -->
		<view class="position-fixed p-bottom p-left p-right p-top black100-50" v-if="showLoding" @tap="close"></view>
		<view class="wid-100  white-bj fuwu-foot position-fixed p-bottom box-sizing" :style="{height:heightFoot+'px'}" v-if="showLoding">
			<view class="position-relative">
				<view class="text-center">{{footTitle}}</view>
				<view class="icon iconfont icon-guanbi2 position-absolute p-right p-top" @tap="close"></view>
			</view>
			<!-- 优惠券 -->
			<view class="mar-top50 coupon-foot" v-if="type=='Desconto'">
				<view class="coupon-item a-center border-1-91 border-r-20 pad-t-30 pad-b-30 box-sizing" v-for="(item,index) in coupon_list"  :key="index">
					<view class="text-center left">
						<view class="main-zt font-28 font-w-b">¥<text class="font-40">{{item.money}}</text></view>
						<view class="font-20 mar-top15" v-if="item.minprice==0">无门券</view>
						<view  class="font-20 mar-top15" v-else>满{{item.minprice}}可用</view>
					</view>
					<view class="right mar-left20">
						<view class="font-24 font-w-b text-overflow">{{item.couponname}}</view>
						<view class="a-center j-sp mar-top20">
							<view class="font-20 main-bj border-r-5 line-h32 pad-l-10 pad-r-10 box-sizing d-inline-block white-zt" v-if="item.type==1">Cupom</view>
							<view class="white-zt border-r btnCoupon font-24 line-h45 main-bj-jb" @tap="receive(item,'add')" v-if="item.minprice<=formData.product_price &&couponData.id!=item.id">使用</view>
							<view class="black60-zt btnCoupon font-24 line-h45 black93-bj" @tap="receive(item,'del')" v-else-if="item.minprice<=formData.product_price &&couponData.id==item.id">不使用</view>
							<view class="black60-zt btnCoupon font-24 line-h45 black93-bj" v-else>使用</view>
						</view>
						<view class="black60-zt font-24 mar-top20">Validade：<text>{{timeS(item.usetime)}}</text></view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				checkbox:false,
				showLoding:false,
				heightFoot:0,
				type:"",
				footTitle:"",
				is_add:false,
				formData:{
					pic:"",
					name:"",
					guige_id:undefined,
					guige_name:"",
					proid:undefined,
					sell_price:0,
					market_price:0,
					business_logo:"",
					bid:0,
					business_name:"",
					mid:undefined,
					scoredkmaxset:0,
					scoredkmaxval:0,
					sum:0,
					product_price:0,
					total_weight:0,
					score:0,
					type:"shop",
					freight_price:0,
					totalprice:0,
					scoredk_money:0,
					coupon_money:0,
					leveldk_money:0,
					coupon_rid:[],
					aid:38,
					freight_name:"",
					freight_id:undefined,
					freight_dataval:"",
					freight_type:0,
					preferential:0,
					scoredkscore:0,
					address_id:0,
					platform:"APP",
					business_total_money:0,
					type:0
				},
				address:{},
				pro:{},
				freight:[],
				coupon_list:[],
				guigeData:{},
				couponData:{},
				score:{
					score:0,
					score2money:0
				},
				freight_formdata:[],
				paylist:[],
				checked:0
			}
		},
		onLoad(e) {
			const form=JSON.parse(e.id)
			const num=parseFloat(form.sum)*parseFloat(form.sell_price)
			this.formData.pic=form.pic,
			this.formData.name=form.name,
			this.formData.guige_id=parseFloat(form.guige_id),
			this.formData.guige_name=form.guige_name,
			this.formData.proid=parseFloat(form.proid),
			this.formData.type=parseFloat(form.type),
			this.formData.sell_price=parseFloat(form.sell_price),
			this.formData.market_price=parseFloat(form.market_price),
			this.formData.type=parseFloat(form.type)
			this.formData.mid=parseFloat(form.mid),
			this.formData.scoredkmaxset=parseFloat(form.scoredkmaxset),
			this.formData.scoredkmaxval=parseFloat(form.scoredkmaxval),
			this.formData.sum=parseFloat(form.sum),
			this.formData.product_price=Math.round(num * 100) / 100,
			this.guigeData=form.guigeData
			this.GetOrderInfo()
			this.getPayList()
		},
		onShow() {
			let that = this
			//接收下级页面传回的数据
			uni.$on('updateData',function(data){
				that.address = data
				that.formData.address_id=data.id
			})
		},
		methods: {
			//打开页面或者弹窗
			open(type){
				switch (type){
					case "add":
						uni.navigateTo({
							url:'/pagesB/address/addressList?type='+1
						})
					break;
				}
			},
			//获取订单信息
			async GetOrderInfo(){
				const one={
					mid:this.formData.mid,
					aid:1,
					id:this.formData.proid,
					type:this.formData.type
				}
				await this.$api.GetConfirmarShopOrder(one).then(res=>{
					this.address=res.data.address
					this.formData.address_id=res.data.address.id
					this.pro=res.data.pro
					this.formData.business_logo=res.data.logo
					this.formData.business_name=res.data.name
					if(res.data.freight!=null){
						this.freight=res.data.freight
						this.formData.freight_id=res.data.freight[0].id
						this.freight_formdata=JSON.parse(res.data.freight[0].formdata)
					}
					//计算运费
					if(res.data.pro.type==2){
						this.freightCalculate()
					}
					//计算总支付金额
					this.Calculate()
					if(res.data.coupon_list!=null){
						this.coupon_list=res.data.coupon_list
					}
					//计算积分抵扣
					this.Deductible(this.formData.product_price)
				})
			},
			//计算金额
			Calculate(){
				//计算等级优惠
				const leveldk_money=this.formData.product_price*(1-parseFloat(this.pro.leveldk_money)/10)
				this.formData.leveldk_money=Math.round(leveldk_money * 100) / 100
				//计算
				const num=this.formData.product_price+this.formData.freight_price-leveldk_money-this.formData.coupon_money-this.formData.scoredk_money//计算订单总金额
				const num1=leveldk_money+this.formData.coupon_money+this.formData.scoredk_money//计算优惠金额
				const num2=this.formData.product_price+this.formData.freight_price//计算商户获取的金额
				this.formData.totalprice=Math.round(num * 100) / 100
				this.formData.preferential=Math.round(num1 * 100) / 100
				this.formData.business_total_money=Math.round(num2 * 100) / 100
			},
			//计算积分抵扣
			Deductible(num){
				//积分计算
				if(this.formData.scoredkmaxset==0){
					const scoredkmaxpercent=parseFloat(this.pro.scoredkmaxpercent)/100//计算商品可用抵扣的金额的比例
					const Deductible=num*scoredkmaxpercent//计算最多可抵扣金额
					const maxscore=Math.floor(Deductible/parseFloat(this.pro.score2money))//计算最多比例的积分需要多少，并且使用Math.floor()向下取整数
					//判断用户拥有的积分是否大于全额抵扣积分
					if(this.pro.score>=maxscore){//大于显示最高所需积分
						this.score.score=maxscore//赋值需要的积分
						this.score.score2money=Math.round(Deductible * 100) / 100//赋值可用抵扣的金额
					}else{//小于显示用户拥有的积分
						this.score.score=this.pro.score//赋值需要的积分
						const score2money=this.pro.score*parseFloat(this.pro.score2money)
						this.score.score2money=Math.round(score2money * 100) / 100//赋值可用抵扣的金额
					}
				}else if(this.formData.scoredkmaxset==1){
					const scoredkmaxpercent=parseFloat(this.pro.scoredkmaxpercent)/100//计算商品可用抵扣的金额的比例
					const Deductible=num*scoredkmaxpercent//计算最多可抵扣金额
					const maxscore=Math.floor(Deductible/parseFloat(this.pro.score2money))//计算最多比例的积分需要多少，并且使用Math.floor()向下取整数
					//判断用户拥有的积分是否大于全额抵扣积分
					if(this.pro.score>=maxscore){//大于显示最高所需积分
						this.score.score=maxscore//赋值需要的积分
						this.score.score2money=Math.round(Deductible * 100) / 100//赋值可用抵扣的金额
					}else{//小于显示用户拥有的积分
						this.score.score=this.pro.score//赋值需要的积分
						const score2money=this.pro.score*parseFloat(this.pro.score2money)
						this.score.score2money=Math.round(score2money * 100) / 100//赋值可用抵扣的金额
					}
				}else if(this.formData.scoredkmaxset==2){
					const Deductible=0//计算最多可抵扣金额
					if(num>=parseFloat(this.pro.scoredkmaxpercent)){
						Deductible=parseFloat(this.pro.scoredkmaxpercent)
					}else{
						Deductible=num
					}
					const maxscore=Math.floor(Deductible/parseFloat(this.pro.score2money))//计算最多比例的积分需要多少，并且使用Math.floor()向下取整数
					//判断用户拥有的积分是否大于全额抵扣积分
					if(this.pro.score>=maxscore){//大于显示最高所需积分
						this.score.score=maxscore//赋值需要的积分
						this.score.score2money=Math.round(Deductible * 100) / 100//赋值可用抵扣的金额
					}else{//小于显示用户拥有的积分
						this.score.score=this.pro.score//赋值需要的积分
						const score2money=this.pro.score*parseFloat(this.pro.score2money)
						this.score.score2money=Math.round(score2money * 100) / 100//赋值可用抵扣的金额
					}
				}
			},
			//计算运费
			freightCalculate(){
				this.freight.forEach(item=>{
					if(this.formData.freight_id==item.id){
						this.formData.freight_type=item.pstype
						this.formData.freight_name=item.name
						let baoyou=false//定义变量，检查是否满足满额包邮活动
						if(item.freeset==1){//判断该物流是否开启了满额包邮
							//在开启了满额包邮情况下，判断满额金额是否小于商品总金额
							if(item.free_price<=this.formData.product_price){
								baoyou=true
							}
						}
						//如果满足满额包邮条件则运费为0，并且结束运行代码
						if(baoyou){
							this.formData.freight_price=0
							return
						}
						let arr=JSON.parse(item.pricedata)
						//计算总重量
						let weight=this.guigeData.weight*this.formData.sum
						this.formData.total_weight=parseInt(weight)
						let num=0
						if(arr.length<=1){
							let price=parseFloat(arr[0].fristprice)
							//判断该商品是否超重
							if(weight<=parseFloat(arr[0].fristweight)){//如果重量在首重范围内，则使用默认
								num=price
							}else{//如果超重则计算
								let fweight=weight-parseFloat(arr[0].fristweight)//减去首重后的重量
								let total_page=fweight/parseFloat(arr[0].secondweight)//剩余重量除以续重重量
								//判断计算后是否为整数
								if(Number.isInteger(total_page)==true){//如果是整数
									total_page=total_page
								}else{//如果不是整数则，进行向上取整的数字
									total_page=Math.ceil(total_page)
								}
								num=price+(total_page*parseFloat(arr[0].secondprice))
							}
						}else{
							//循环所有的物流配置
							arr.forEach(element=>{
								let region=element.region.split(';')//分割地址
								region.forEach(n=>{//循环地址
									//判断当前的地址是否为选中的省
									if(n==this.address.province){
										let price=parseFloat(element.fristprice)
										//判断该商品是否超重
										if(weight<=parseFloat(element.fristweight)){//如果重量在首重范围内，则使用默认
											num=price
										}else{//如果超重则计算
											let fweight=weight-parseFloat(element.fristweight)//减去首重后的重量
											let total_page=fweight/parseFloat(element.secondweight)//剩余重量除以续重重量
											//判断计算后是否为整数
											if(Number.isInteger(total_page)==true){//如果是整数
												total_page=total_page
											}else{//如果不是整数则，进行向上取整的数字
												total_page=Math.ceil(total_page)
											}
											num=price+(total_page*parseFloat(element.secondprice))
										}
									}
								})
							})
						}
						this.formData.freight_price=Math.round(num * 100) / 100
					}
				})
			},
			//获取支付列表
			async getPayList(){
				const one={
					aid:1,
					state:1
				}
				await this.$api.GetPayList(one).then(res=>{
					this.checked=res.data.list[0].id
					this.paylist=res.data.list
				})
			},
			//选中优惠券
			receive(data,type){
				if(type=="add"){
					this.formData.coupon_rid.push(data.id)
					this.formData.coupon_money=data.money
					this.couponData=data
					this.close()
				}else{
					this.formData.coupon_rid=[]
					this.formData.coupon_money=0
					this.couponData={}
				}
				this.Calculate()
				
			},
			//时间格式
			timeS(data){
				return time.gettime.timeStanp(data)
			},
			//打开弹窗
			openTan(type,shopTyoe){
				switch (type){
					case "Desconto":
					this.footTitle="选择优惠券"
					this.type="Desconto"
					this.heightFoot=580
					break
				}
				this.showLoding=true
			},
			//关闭弹窗
			close(){
				this.type=""
				this.showLoding=false
			},
			//复选按钮
			change(e){
				this.checkbox=!this.checkbox
				if(this.checkbox){
					this.formData.scoredk_money=this.score.score2money
					this.formData.scoredkmaxval=this.score.score
					this.formData.scoredkscore=this.score.scoredkscore
				}else{
					this.formData.scoredk_money=0
					this.formData.scoredkmaxval=0
					this.formData.scoredkscore=0
				}
				this.Calculate()
			},
			//监听选择支付方式
			changePay(e){
				this.checked=parseFloat(e.detail.value)
			}
		}
	}
</script>

<style lang="scss">
	body{
	    background-color: #f6f6f6;  
	}  
	image{
		width: 100%;
		display: block;
	}
	.address{
		border-bottom-left-radius: 25rpx;
		border-bottom-right-radius: 25rpx;
		.wu{
			.icon-position{
				font-size: 40rpx;
			}
		}
		.you{
			.icon-jinrujiantou1{
				font-size: 40rpx;
			}
			.icon-dingwei{
				font-size: 50rpx;
			}
		}
		
	}
	.shop{
		border-radius: 25rpx;
		.logo{
			width: 40rpx;
			height: 40rpx;
		}
		.left{
			width: 175rpx;
			height: 175rpx;
		}
		.right{
			width: 505rpx;
		}
		textarea{
			height: 150rpx;
			padding: 20rpx 30rpx;
		}
	}
	.ljm-checkbox{
		width: 25rpx;
		height: 25rpx;
		border: 2rpx solid #999999;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.ljm-checkbox.ljm-checkbox-active{
		background-color: #e5353f;
		border: 2rpx solid #e5353f;
	}
	.ljm-checkbox .icon-duihao{
		font-size: 20rpx;
	}
	.fuwu-foot{
		border-top-left-radius: 35rpx;
		border-top-right-radius: 35rpx;
		padding: 30rpx 50rpx;
		.coupon-foot{
			max-height: 400rpx;
			.coupon-item{
				.left{
					width: 200rpx;
					border-right: 2rpx solid #E7E7E7;
				}
				.right{
					.btnCoupon{
						padding: 0 30rpx;
						background-color: #DDDDDD;
					}
				}
			}
		}
	}
	.pay-box{
		.item{
			.img{
				width: 45rpx;
				height: 45rpx;
			}
			label{
				padding-right: 0rpx;
			}
		}
	}
</style>
